package com.tripspins.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tripspins.app.bo.Attachment;
import com.tripspins.app.bo.Pin;
import com.tripspins.app.helper.DbHelper;
import com.tripspins.app.helper.FILE_TYPE;
import com.tripspins.app.helper.MapsAPIHelper;
import com.facebook.widget.ProfilePictureView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
//import com.google.maps.android.ui.IconGenerator;

/**
 * Created by João.Rutkoski on 06/02/14.
 */
public class PinDetailsActivity extends Activity {

    TextView pinTitle;
    TextView pinNote;
    TextView pinAddress;
    TextView pinWeatherDegrees;
    LinearLayout scrollAttachments;
    ImageView pinWeatherIcon;
    DbHelper dbHelper;
    GoogleMap map;
    final Activity activity = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_map_details);

        dbHelper = new DbHelper(this);


        pinTitle = (TextView)findViewById(R.id.mapTitle);
        pinNote =  (TextView)findViewById(R.id.mapNote);
        pinAddress =  (TextView)findViewById(R.id.tripAddress);
        pinWeatherDegrees =  (TextView)findViewById(R.id.mapWeatherDegrees);
        scrollAttachments = (LinearLayout)findViewById(R.id.mapAttachmentsList);
        pinWeatherIcon = (ImageView)findViewById(R.id.mapWeatherImg);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        Intent myIntent = getIntent();
        Pin pin = myIntent.getParcelableExtra("pin");
        long tripId = myIntent.getLongExtra("tripId",0);

        pinTitle.setText(pin.getTitle());
        pinNote.setText(pin.getTextNote());
        pinAddress.setText(pin.getAddressString());
        pinWeatherDegrees.setText(pin.getCurrentWeather().getTempC() + "ºC");
        pinWeatherIcon.setImageURI(Uri.fromFile(pin.getWeatherIconUri(this, tripId)));
        loadAttachmentsThumbnails(pin);
        //IconGenerator iconGenerator = new IconGenerator(this);
        //MarkerOptions markerOptions = MapsAPIHelper.getMarker(pin, iconGenerator);
        MarkerOptions markerOptions = MapsAPIHelper.getMarker(pin);
        Marker marker = map.addMarker(markerOptions);
    }

    private void loadAttachmentsThumbnails(Pin p) {
        scrollAttachments.removeAllViews();
        ImageView thumbnail;
        for(Attachment a : p.getAttachments())
        {
            if(a.getPath().compareTo("") != 0){
                final Attachment att = a;
                thumbnail = new ImageView(this);
                thumbnail.setPadding(0,3,3,0);
                thumbnail.setImageURI(Uri.parse(a.getThumbnailPath()));
                thumbnail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.setAction(android.content.Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse("file://" + att.getPath()), "image/*");
                        activity.startActivity(intent);
                    }
                });
                thumbnail.setMaxHeight(10);
                thumbnail.setMaxWidth(10);
                scrollAttachments.addView(thumbnail);
            }
            else if(a.getFileType() == FILE_TYPE.CONTACT && a.getContact().getContactIdOnSource()!= null)
            {
                ProfilePictureView fbContactPiture = new ProfilePictureView(this);

                fbContactPiture.setPadding(0,3,3,0);
                fbContactPiture.setProfileId(a.getContact().getContactIdOnSource());
                scrollAttachments.addView(fbContactPiture);
            }

        }
    }

}
